package com.ruoyi.eqmonitor.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import cn.hutool.core.collection.ListUtil;
import com.ruoyi.eqmonitor.dtos.*;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.ApiOperation;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.eqmonitor.domain.OptPatrolEvent;
import com.ruoyi.eqmonitor.service.IOptPatrolEventService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;

import java.util.Arrays;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.security.utils.SecurityUtils;

import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 巡查上报事件
 *
 * @author ruoyi
 * @date 2022-12-05
 */
@RestController
@RequestMapping("/patrolEvent")
public class OptPatrolEventController extends BaseController {

    @Autowired
    private IOptPatrolEventService optPatrolEventService;

    /**
     * 巡查事件列表 所有事件
     */
    @ApiOperation("巡查事件列表 所有事件")
    @GetMapping("/listAll")
    public TableDataInfo<?> listAll(OptPatrolEvent optPatrolEvent) {
        startPage();
        return getDataTable(optPatrolEventService.selectOptPatrolEventList(optPatrolEvent));
    }

    /**
     * 巡查事件列表 当前用户的事件
     */
    @ApiOperation("巡查事件列表 当前用户的事件")
    @GetMapping("/list")
    public TableDataInfo<?> list(OptPatrolEvent optPatrolEvent) {
        startPage();

        LoginUser user = SecurityUtils.getLoginUser();
        if (user == null) return getDataTable(ListUtil.empty());
        long userid = user.getUserid();
        var roles = user.getRoles();
        if (!roles.contains("admin")) {
            //获取当前登录人员,赋值任务接收人为当前登录人员
            optPatrolEvent.setObjectId(user.getUserid());
        }
        List<OptPatrolEvent> list = optPatrolEventService.selectOptPatrolEventList(optPatrolEvent);
        return getDataTable(list);
    }

    /**
     * 根据id查询巡查事件
     */
    @ApiOperation("根据id查询巡查事件")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(optPatrolEventService.selectOptPatrolEventById(id));
    }

    /**
     * 巡查上报事件
     */
    @ApiOperation("巡查上报事件")
    @Log(title = "巡查上报事件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OptPatrolEvent optPatrolEvent) {
        return toAjax(optPatrolEventService.add(optPatrolEvent));
    }

    /**
     * 指派——处置单位，事件受理
     */
    @ApiOperation("指派——处置单位，事件受理")
    @PutMapping("/assign")
    public AjaxResult assign(@RequestBody OptPatrolEventAssignDto optPatrolEventAssignDto) {
        return toAjax(optPatrolEventService.assign(optPatrolEventAssignDto));
    }

    /**
     * 派遣——处置人员，派遣人员
     */
    @ApiOperation("派遣——处置人员，派遣人员")
    @PutMapping("/dispatch")
    public AjaxResult dispatch(@RequestBody OptPatrolEventDispatchDto optPatrolEventDispatchDto) {
        return toAjax(optPatrolEventService.dispatch(optPatrolEventDispatchDto));
    }

    /**
     * 事件确认
     */
    @ApiOperation("事件确认")
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody OptPatrolEventConfirmDto optPatrolEventConfirmDto) {
        return toAjax(optPatrolEventService.confirm(optPatrolEventConfirmDto));
    }

    /**
     * 事件处置
     */
    @ApiOperation("事件处置")
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody OptPatrolEventHandleDto optPatrolEventHandleDto) {
        return toAjax(optPatrolEventService.handle(optPatrolEventHandleDto));
    }

//    /**
//     * 事件核查
//     */
//    @PutMapping("/check")
//    public AjaxResult check(@RequestBody OptPatrolEventCheckDto optPatrolEventCheckDto){
//
//        OptPatrolEvent optPatrolEvent = optPatrolEventService.getById(optPatrolEventCheckDto.getEvtId());
//
//        return toAjax(optPatrolEventService.updateById(optPatrolEvent));
//    }

    /**
     * 审核反馈——主管单位
     */
    @ApiOperation("审核反馈——主管单位")
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody OptPatrolEventAuditDto optPatrolEventAuditDto) {
        return toAjax(optPatrolEventService.audit(optPatrolEventAuditDto));
    }

    /**
     * 事件完结
     */
    @ApiOperation("事件完结")
    @PutMapping("/end")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult end(@RequestBody OptPatrolEventEndDto optPatrolEventEndDto) {
        return toAjax(optPatrolEventService.end(optPatrolEventEndDto));
    }

    /**
     * 修改事件
     */
    @ApiOperation("修改事件")
    @PutMapping
    public AjaxResult edit(@RequestBody OptPatrolEvent optPatrolEvent) {
        optPatrolEvent.setUpdateTime(new Date());
        optPatrolEvent.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(optPatrolEventService.updateById(optPatrolEvent));
    }

    /**
     * 删除事件
     */
    @ApiOperation("删除事件")
    @Log(title = "巡查上报事件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(optPatrolEventService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 导出巡查上报事件列表
     */
    @ApiOperation("导出巡查上报事件列表")
    @Log(title = "巡查上报事件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OptPatrolEvent optPatrolEvent) {
        List<OptPatrolEvent> list = optPatrolEventService.selectOptPatrolEventList(optPatrolEvent);
        ExcelUtil<OptPatrolEvent> util = new ExcelUtil<OptPatrolEvent>(OptPatrolEvent.class);
        util.exportExcel(response, list, "巡查上报事件数据");
    }

}
